package com.cyq.day21.managementsystem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AdminManagement extends JFrame {
    private JPanel panel;
    private JTextArea textArea;
    private JTextField textField;
    private JButton button;
    String data = "";

    public AdminManagement(Employee[] employees, int index) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("管理员界面");
        textArea = new JTextArea();
        textArea.setText("欢迎使用员工管理" + "\n"
                + "请输入选项：" + "\n"
                + "1.查看所有员工" + "\n"
                + "2.添加员工" + "\n"
                + "3.删除员工" + "\n"
                + "4.修改密码" + "\n"
                + "5.安全退出" + "\n");
        add(textArea);
        panel = new JPanel();
        textField = new JTextField(20);
        button = new JButton("确定");
        panel.add(textField);
        panel.add(button);
        add(BorderLayout.SOUTH, panel);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if ("".equals(textField.getText())) {
                    JOptionPane.showMessageDialog(null, "请输入正确的选项");
                }
                //查看员工
                if ("1".equals(textField.getText())) {
                    for (int i = 0; i < employees.length; i++) {
                        if (employees[i] != null) {
                            data += employees[i].getName() + "\t" + employees[i].getLevel() + "\n";
                        }
                    }
                    JOptionPane.showMessageDialog(null, "姓名" + "\t" + "等级" + "\t\n"
                            + data);
                    data = "";
                }
                //添加员工
                if ("2".equals(textField.getText())) {
                    String inputName = JOptionPane.showInputDialog(null, "请输入员工姓名");
                    String inputLevel = JOptionPane.showInputDialog(null, "请输入员工等级(管理员/员工)");
                    if ("".equals(inputName) || "".equals(inputLevel)) {
                        JOptionPane.showMessageDialog(null, "姓名或员工等级不能为空");
                    } else {
                        for (int i = 0; i < employees.length; i++) {
                            if (employees[i] == null) {
                                employees[i] = new Employee();
                                employees[i].setName(inputName);
                                employees[i].setPassword("666");
                                employees[i].setLevel(inputLevel);
                                JOptionPane.showMessageDialog(null, "添加成功");
                                return;
                            }
                        }
                    }

                }
                //删除员工
                if ("3".equals(textField.getText())) {
                    String inputDel = JOptionPane.showInputDialog(null, "请输入要删除的员工姓名");
                    boolean flag = false;
                    for (int i = 0; i < employees.length; i++) {
                        if (employees[i].getName().equals(inputDel)) {
                            employees[i] = employees[i + 1];
                            JOptionPane.showMessageDialog(null, "删除成功");
                            flag = true;
                        }
                    }
                    if (flag) {
                        for (int i = 0; i < employees.length; i++) {
                            if (employees[i] != null) {
                                data += employees[i].getName() + "\t" + employees[i].getLevel() + "\n";
                            }
                        }
                        JOptionPane.showMessageDialog(null, "姓名" + "\t" + "等级" + "\t\n"
                                + data);
                    } else {
                        JOptionPane.showMessageDialog(null, "查无此人");

                    }
                }
                //修改密码
                if ("4".equals(textField.getText())) {
                    String inputpwd = JOptionPane.showInputDialog(null, "请输入旧密码");
                    if (inputpwd.equals(employees[index].getPassword())) {
                        String inputpwd1 = JOptionPane.showInputDialog(null, "请输入新密码");
                        String inputpwd2 = JOptionPane.showInputDialog(null, "请输入新密码");
                        if (inputpwd1.equals(inputpwd2)) {
                            employees[index].setPassword(inputpwd1);
                            JOptionPane.showMessageDialog(null, "密码修改成功");
                        } else {
                            JOptionPane.showMessageDialog(null, "两次密码不一致");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "旧密码错误");
                    }
                }
                //安全退出
                if ("5".equals(textField.getText())) {
                    ChooseFrame chooseFrame = new ChooseFrame();
                    chooseFrame.setVisible(true);
                    AdminManagement.this.dispose();
                }

            }
        });

    }
}